package com.base.library.http;

import android.content.Context;
import android.os.Build;

import com.base.library.manager.RealmManager;
import com.base.library.utils.PackageUtils;

import java.io.IOException;

import io.realm.Realm;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HeaderInterceptor implements Interceptor {
    private Context mContext;
    private String imei = "-";
    private String ver = "-";
    private String token = "-";
    private String uid = "-";
    private int build;
    private RealmManager realmManager;

    public HeaderInterceptor(Context context) {
        mContext = context;
        imei = PackageUtils.getIMEI(mContext.getApplicationContext());
        ver = PackageUtils.getVersionName(mContext);
        build = PackageUtils.getVersionCode(mContext);
        realmManager = new RealmManager(Realm.getDefaultInstance());
    /*    UserBean userBean = realmManager.findFirst(UserBean.class);
        if (userBean != null) {
            token = userBean.token;
            uid = userBean.id;
        }*/
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        Request.Builder requestBuilder = original.newBuilder()
                .addHeader("device", Build.MODEL)
                .addHeader("imei", imei)
                .addHeader("uid", uid)
                .addHeader("token", token)
                .addHeader("ostype", "Android")
                .addHeader("osver", Build.VERSION.RELEASE)
                .addHeader("appver", ver)
                .addHeader("appbuild", build + "");
        Request request = requestBuilder.build();
        return chain.proceed(request);
    }
}
